/**
 * Created by billy on 2018/9/18.
 */
var AipSpeechClient = require("baidu-aip-sdk").speech;

// 设置APPID/AK/SK
var APP_ID = "9854110";
var API_KEY = "e808pvBGQWG0QYMKpypY0sDO";
var SECRET_KEY = "fYoowqXGnF8H8aKYXTNkdN1WkKoxjeD3";

// 新建一个对象，建议只保存一个对象调用服务接口
var client = new AipSpeechClient(APP_ID, API_KEY, SECRET_KEY);

var fs = require('fs');
var count = 1;
var param = {
	spd: 3,
	per: 0,
	pit: 6
}

//2 4 10  宝宝真棒

var list = [];
var id = "";

function getList($list, $obj)
{
	list = $list;
	id = $obj.id;
	goNext();
}

exports.getList = getList;

function goNext()
{
	if (list.length > 0)
	{
		if (list[0])
		{
			doVoice(list[0]);
		}
		else
		{
			end();
		}
	}
}

function end()
{
	list.shift();
	console.log(list.length);
	count++;
	goNext();
}

function doVoice($voice)
{
	client.text2audio($voice, param).then(function (result) {
		if (result.data)
		{
			fs.writeFileSync(`game/${id}/dv1_${count}.mp3`, result.data);
			end();
		}
		else
		{
			console.log(result)
		}
	}, function (e) {
		console.log(e)
	});
}